import { observable, action } from 'mobx';
import * as Apis from '../../apis/Api';

export default class ApiSingleShowStore {
    @observable apiId = '';// 接口id
    @observable baseList;// 基本信息
    @observable tableList;// 列表
    @observable showTemplet;// 显示模板
    @observable isSuccess = false;// 是否成功数据
    @observable urlQuery;// 拼接完后的参数
    @observable groups = [];// 菜单分组
    @observable params = [];// 改接口设置的参数
    @observable singRequestParam = {};// 单接口页面接收到的参数
    @observable subTableList = [];// 子列表
    @observable apiName = '';// 接口名称
    @observable front_path = '';// 前端路径
    @observable updata_date = '';// 数据更新时间
    @observable msgcode = '';// 返回信息
    @observable loading = true;// 加载状态
    @observable isList = '0';// 接口内的数据为纯列表
    @observable thisPage = 1;// 纯列表类接口默认显示第一页，启信宝显示前二十条
    @observable total = 1;// 纯列表类接口一共有多少页
    @observable module = '';
    @observable info = [];
    @observable apiRemark = '';
    @observable apiJson = '';
    @observable showAlertIcon = false;
    @observable isGetListByName = true;// 如果第一次查询后没有返回的数据,经过后台数据处理后再去查询第二遍,并返回模糊匹配列表
    @observable showSpin = true;
    @action
    init = (comBj) => {
      // console.log(comBj)
      if (comBj.name) {
        this.apiName = comBj.name;
      }
      if (comBj.showAlertIcon) {
        this.showAlertIcon = comBj.showAlertIcon;
      }
      if (comBj.params.apiId) {
        this.apiId = comBj.params.apiId;
      }
      if (comBj.isList) {
        this.isList = comBj.isList;
      }
      if (typeof (comBj.showSpin) !== 'undefined') {
        this.showSpin = comBj.showSpin;
      }
      this.params = comBj.params;
      this.callApi();
    }
    // 调用API
    @action
    callApi = () => {
      this.loading = true;
      this.isSuccess = false;
      Apis.getApi({ ...this.params, isList: this.isList, skip: this.skip }).then((data) => {
        // console.log(data);
        if (data.status == 'success') {
          if (data.datas.baseList) {
            this.baseList = data.datas.baseList;
          }
          this.apiJson = data.json;
          this.tableList = data.datas.tableList;
          this.subTableList = data.datas.sublist;
          this.updata_date = `${data.updata_date} ${data.updata_time}`;
          this.total = Math.ceil(parseInt(data.datas.total) / 20);
          this.totalNumber = parseInt(data.datas.total);
          this.isSuccess = true;
        } else {
          this.msgcode = data.msgcode;
          this.isSuccess = false;
        }
        this.loading = false;
      });
    }

    // 翻页
    onChangePage = (page) => {
      this.clrarCatch();
      this.thisPage = page;
      this.skip = (page - 1) * 20;
      // console.log("this.skip :" + this.skip);
      this.callApi();
    }

    // 清除缓存
    @action
    clrarCatch = () => {
      this.msgcode = '';
      this.updata_date = '';
      this.isSuccess = false;
      this.loading = true;
    }
}
